<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="年级" prop="grade">
      <el-radio v-model="dataForm.grade" label="1">大一上</el-radio>
      <el-radio v-model="dataForm.grade" label="2">大一下</el-radio>
      <el-radio v-model="dataForm.grade" label="3">大二上</el-radio>
      <el-radio v-model="dataForm.grade" label="4">大二下</el-radio>
      <el-radio v-model="dataForm.grade" label="5">大三上</el-radio>
      <el-radio v-model="dataForm.grade" label="6">大三下</el-radio>
    </el-form-item>
    <el-form-item>
      <el-radio v-model="dataForm.grade" label="7">大四上</el-radio>
      <el-radio v-model="dataForm.grade" label="8">大四下</el-radio>
    </el-form-item>
    <el-form-item label="课程" prop="courses">
      <el-select v-model="courseArray" multiple placeholder="请选择" @focus="selectTeacher">
      <el-option
        v-for="item in courses"
        :key="item.id"
        :label="item.name"
        :value="item.id">
      </el-option>
  </el-select>
    </el-form-item>
    
    <el-form-item label="专业ID" prop="majorId">
      <el-select v-model="dataForm.majorId" placeholder="请选择" @focus="selectMajor">
      <el-option
        v-for="item in majors"
        :key="item.id"
        :label="item.name"
        :value="item.id">
      </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          courses: '',
          grade: '',
          majorId: '',
          createTime: '',
          updateTime: '',
          isDelete: ''
        },
        majors: [],
        courses: [],
        courseArray: [],
        dataRule: {
          grade: [
            { required: true, message: '年级不能为空', trigger: 'blur' }
          ],
          majorId: [
            { required: true, message: '专业ID不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      selectMajor(){
        this.$http({
              url: this.$http.adornUrl(`/stu/major/listNoPage`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.majors = data.majors
              }
            });
      },
      selectTeacher(){
        if(this.dataForm.grade){
          this.$http({
              url: this.$http.adornUrl(`/edm/course/selectCourseByGrade`),
              method: 'get',
              params: this.$http.adornParams({
                'grade': this.dataForm.grade
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.courses = data.course
              }
            });
        }else{
          this.teachers = [];
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.courseArray = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/edm/template/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courses = data.template.courses
                this.dataForm.grade = data.template.grade
                this.selectTeacher()
                this.dataForm.courses.split(",").forEach(item => {
                  this.courseArray.push(parseInt(item))
                })
                
                this.dataForm.majorId = data.template.majorId
                this.dataForm.createTime = data.template.createTime
                this.dataForm.updateTime = data.template.updateTime
                this.dataForm.isDelete = data.template.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/edm/template/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'courses': this.courseArray.join(","),
                'grade': this.dataForm.grade,
                'majorId': this.dataForm.majorId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
